package com.lin.data.jpa.dao;

import com.lin.data.jpa.model.entity.SysUser;
import com.lin.data.jpa.model.other.UserInterface;
import com.lin.data.jpa.model.other.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.util.Streamable;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author: linjiang
 * @date: 2025/2/12
 */
public interface SysUserDao extends JpaRepository<SysUser, Integer>, JpaSpecificationExecutor<SysUser> {

    /**
     * 自定义注解查询语句
     * 1.相当于mybatis中的@Select注解查询
     *
     * @return
     */
    @Query(value = "select id,name from SysUser")
    List<Map<String, Object>> customQuery();

    /**
     * JPA内置识别 - 生成查询语句
     *
     * @param name
     * @return
     */
    Integer countByName(String name);

    /**
     * JPA内置识别 - 生成删除语句
     * 1.需要添加事务注解 - 否则会报错，无法删除
     *
     * @param name
     * @return
     */
    @Transactional
    Long deleteByName(String name);

    /**
     * JPA内置识别 - 生成查询语句 -> 转变为流
     *
     * @param name
     */
    Streamable<SysUser> findByName(String name);

    Users queryByName(String name);

    /**
     * 测试实体表达式
     *
     * @param name
     */
    @Query("select u from #{#entityName} u where u.name = :name")
    SysUser listByName(@Param("name") String name);

    /**
     * 测试值表达式
     *
     * @param name
     */
    @Query("select u from #{#entityName} u where u.name = ?1 or u.name = ?#{[0]}")
    SysUser listByName01(String name);

    /**
     * 测试结果集映射
     *
     * @param name
     */
    @Query("select u from #{#entityName} u where u.name = ?1")
    UserInterface listByName02(String name);


}
